using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Slaks.Preprocessing
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class USeriesPreprocessing
	{
    private string m_outputDirectory = "c:\\temp";
		private string m_sourceDirectory = String.Empty;
		private string [] m_sourceFiles = null;

		
		public USeriesPreprocessing(string sourceDirectory)
		{
			m_sourceDirectory = sourceDirectory;
			m_sourceFiles = Directory.GetFiles(m_sourceDirectory);
		}
		public USeriesPreprocessing(string sourceDirectory,string outputDirectory)
		{
			m_sourceDirectory = sourceDirectory;
			m_sourceFiles = Directory.GetFiles(m_sourceDirectory);
			OutputDirectory = outputDirectory;
		}

		public string OutputDirectory
		{
			get { return m_outputDirectory; }
			set { 
				m_outputDirectory= value; 
				if (m_outputDirectory[m_outputDirectory.Length - 1] != '\\') m_outputDirectory += "\\";
			}
		}

		public void Preprocess()
		{
			string filename;
			string destinationContent;
			foreach(string path in m_sourceFiles)
			{
				FileInfo info = new FileInfo(path);
				filename = info.Name;
				destinationContent = this.RemoveNoise(path);
				try 
				{
					WriteNewContent(m_outputDirectory,filename,destinationContent);
				}
				catch(Exception ex)
				{
					Console.WriteLine(filename + " : " + ex.Message);
				}

			}
		}

		private void WriteNewContent(string outputPath, string fileName, string content)
		{
      StreamWriter writer =  new StreamWriter(outputPath + fileName);
			writer.Write(content);
			writer.Close();
		}

		private string RemoveNoise(string file)
		{
      StreamReader reader = File.OpenText(file);
			string fileSource = reader.ReadToEnd();

			string fileDestination;
		
			int index;

			index = fileSource.IndexOf("<!doctype");
			if (index < 0) fileSource.IndexOf("<!DOCTYPE");

			if (index < 0)
			{
				index = fileSource.IndexOf("<html>");
				if (index < 0) index = fileSource.IndexOf("<HTML>");

				if (index < 0)
				{

					index = fileSource.IndexOf("<head>");
					if (index < 0) index = fileSource.IndexOf("<HEAD>");

					if (index < 0)
					{

						index = fileSource.IndexOf("<title>");
						if (index < 0) index = fileSource.IndexOf("<TITLE>");

						if (index < 0)
						{
							index = fileSource.IndexOf("<body>");
							if (index < 0) index = fileSource.IndexOf("<BODY>");

							if (index >= 0)
								fileDestination = fileSource.Substring(index);
							else fileDestination = fileSource;
						}
						else
						{
							fileDestination = fileSource.Substring(index);
						}
					}
					else
					{
						fileDestination = fileSource.Substring(index);
					}
				}
				else
				{
	         fileDestination = fileSource.Substring(index);
				}
			}
			else 
			{
        fileDestination = fileSource.Substring(index);
			}


			reader.Close();
			return fileDestination;
		}
	}
}
